<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/18
 * Time: 22:00
 */

namespace App\Admin\Controllers;

use App\Admin\Traits\UserTrait;
use App\Helper;
use App\Http\Controllers\Controller;
use App\Lib\Sdk\Manager;
use App\Models\GroupConfig;
use App\Models\Roles;
use App\Models\RoleUser;
use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;


class AgentsController extends Controller
{
    use UserTrait;

    public function index(Content $content)
    {
        $this->checkPid();
        return $content
            ->title("代理列表")
            ->body($this->grid());
    }

    private function checkPid()
    {
        return back();
        /*$users = User::query()->where("id", ">", 1)->get();
        $sdk = Manager::getSpider(Manager::JD);
        foreach ($users as $user) {
            if (empty($user->jd_pid)) {
                $pid = $sdk->createPid($user->id);
                $user->jd_pid = $pid;
                $user->save();
            }
        }*/
    }

    public function grid()
    {
        $model = new User();
        $grid = new Grid($model);
        $grid->model()->where("id", ">", 1);
        //$grid->disableCreateButton();
        $grid->disableExport();
        Helper::gridLoadModel($grid, $model);
        $grid->actions(function(Grid\Displayers\Actions $actions) {
           $actions->disableView();
           $actions->disableDelete();
        });
        return $grid;
    }

    public function create(Content $content)
    {
        $model = new User();
        $content
            ->title("代理管理")
            ->body($this->form($model));
        return $content;
    }


    public function edit($id, Request $request, Content $content)
    {
        /** @var Model $model */
        $model = User::where("id", $id)->first();
        if ($request->getMethod() == "GET") {
            $form = new Form($model);
            Helper::formLoadModel($form, $model, $disabled = ['password']);
            $form->setTitle("编辑代理");
            return $content->body($form);
        } else {
            $data = $request->post();
            unset($data['password']);
            /*if (empty($model->pdd_pid)) {
                $pddSpider = Manager::getSpider(Manager::PDD);
                $pddPid = $pddSpider->createPid($model->id);
                $data['pdd_pid'] = $pddPid;
            }
            if (empty($model->jd_pid)) {
                $jdSpider = Manager::getSpider(Manager::JD);
                $pid = $jdSpider->createPid($model->id);
                $data['jd_pid']  = $pid;
            }*/

            $model->update($data);
            return back();
        }

    }

    public function store(Request $request)
    {
        $data = $request->post();
        /** @var User $model */
        $model = new User();
        $model->setAttribute("username", $data['username']);
        $model->setAttribute("password", bcrypt($data['password']));
        $model->setAttribute("mobile", $data['mobile']);
        $model->setAttribute("name", $data['username']);
        $res = $model->saveOrFail();
        if ($res) {
            $manager = Manager::getSdk();
            $robots = $manager->getByUid($model->id);
            if (empty($robots)) {
                $manager->createRobot($model->id);
            }
            $jdSpider = Manager::getSpider(Manager::JD);
            $pid = $jdSpider->createPid($model->id);

            $pddSpider = Manager::getSpider(Manager::PDD);
            $pddPid = $pddSpider->createPid($model->id);
            $model->pdd_pid = $pddPid;
            $model->jd_pid = $pid;

            $role = Roles::where("slug", "agent")->first();
            $roleUser = new RoleUser();
            $roleUser->user_id = $model->id;
            $roleUser->role_id = $role->id;
            $roleUser->save();
            GroupConfig::createGc($model->id);
        }
        return back();
    }

    protected function form($model)
    {
        $form = new Form($model);
        $form->setTitle("新增代理");
        Helper::formLoadModel($form, $model);
        $form->setAction("/admin/agents/create");
        return $form;
    }


}